package com.hdu.other.binarySearch;

import java.io.*;

public class robotJump {


    private static final int MAX_N = 100000;
    private static final int[] ARR = new int[MAX_N];

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
        StreamTokenizer st = new StreamTokenizer(br);

        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            int n = (int) st.nval;
            for (int i = 0; i < n; i++) {
                st.nextToken();
                ARR[i] = (int) st.nval;
            }
            pw.println(compute(n));
        }

        pw.flush();
        pw.close();
        br.close();
    }

    private static int compute(int n) {
        int l = Integer.MAX_VALUE, r = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            l = Math.min(l, ARR[i]);
            r = Math.max(r, ARR[i]);
        }
        int max = r;
        int ans = 0;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (canPass(n, mid, max)) {
                ans = mid;
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return ans;
    }


    static boolean canPass(int n, int energy, int max) {
        for (int i = 0; i < n; i++) {
            if (energy < ARR[i]) {
                energy -= ARR[i] - energy;
            } else {
                energy += energy - ARR[i];
            }
            if (energy >= max) {
                return true;
            }
            if (energy < 0) {
                return false;
            }
        }
        return energy >= 0;
    }

}
